var createError = require('http-errors');
var express = require('express');
var path = require('path');
const cors=require('cors')
// var cookieParser = require('cookie-parser');
// var logger = require('morgan');
//  引入路由器
// const aboutRouter=require('./routes/index')
// const newsRouter=require('./routes/user')
const loginRouter = require('./routes/login')
const updatepwdRouter=require('./routes/updatepwd')
const memberRouter=require('./routes/member')
const customerRouter=require('./routes/customer')
const partmessageRouter=require('./routes/partmessage')
const maintenanceRouter=require('./routes/maintenance')
//  创建WEB服务器
var app = express();
/* app.listen(3000,()=>{
  console.log('服务器启动成功');
}) */
/* 
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
 */
//  中间件
// app.use(logger('dev'));// 打印请求日志
app.use(cors());  //使用cors中间件,解决跨域问题
//post传参转对象
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//缓存
// app.use(cookieParser());
//托管静态资源
app.use(express.static(path.join(__dirname, 'public')));//path.join会生成绝对路径
//注册路由器
app.use('/login', loginRouter)
app.use('/updatepwd',updatepwdRouter)
app.use('/member',memberRouter)
app.use('/customer',customerRouter)
app.use('/partmessage',partmessageRouter)
app.use('/maintenance',maintenanceRouter)
// app.use('/v1/index',indexRouter)
// app.use('/v1/user',userRouter)
//  处理404中间件
// catch 404 and forward to error handler
app.use(function (req, res, next) {
  res.send({ code: 400, msg: '资源不存在' })
});
//  错误处理中间件
// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  /* res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {}; */
  console.log(err)
  res.send({ code: 500, msg: '服务器端错误' })
  // render the error page
  /* res.status(err.status || 500);
  res.render('error'); */
});

module.exports = app;
